package com.aaa.service.impl;

import com.aaa.dao.RoleDao;
import com.aaa.entity.Role;
import com.aaa.service.RoleService;
import com.aaa.util.AccountName;
import com.aaa.vo.Result;
import com.aaa.vo.RoleVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @BelongsPackage: com.aaa.service.impl
 * @Description:
 * @CreateTime: 2023-03-23  17:48
 * @Author: ql
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;
    @Override
    public Result<Map<String,Object>> queryRoleByUserId(Long userId) {
        List<Role> userRoles = roleDao.selectByUserId(userId);
        QueryWrapper<Role> wrapper=new QueryWrapper<>();
        wrapper.eq("status",0);
        List<Role> roles = roleDao.selectList(wrapper);

        Map<String,Object> map=new HashMap<>();
        map.put("roles",roles);
        map.put("userRoles",userRoles);
        return new Result<>(2000,"查询用户角色",map);
    }

    @Transactional
    @Override
    public Result<String> confirmRole(Long userId, Long[] roleIds) {
//        String token = WebUtil.getRequest().getHeader("token");
//        Map<String, Object> tokenInfo = JwtUtil.getTokenChaim(token);
//        String username = (String) tokenInfo.get("username");
//        System.out.println("username = " + username);
//        System.out.println("======= " + userId);
//        System.out.println("======= " + Arrays.toString(roleIds));

        roleDao.deleteByUserId(userId);

        Integer integer = roleDao.insertBatch(userId, roleIds);
        System.out.println("添加权限数============= " + integer);
        if (integer>0){
            return Result.success();
        }else {
            return Result.error();
        }
    }

    @Override
    public Result<IPage<Role>> getByPage(Integer currentPage, Integer pageSize, RoleVo roleVo) {
        IPage<Role> page=new Page<>(currentPage,pageSize);
        QueryWrapper<Role> wrapper=new QueryWrapper<>();
        if(StringUtils.hasText(roleVo.getRoleName())){
            wrapper.like("role_name",roleVo.getRoleName());
        }
        if(StringUtils.hasText(roleVo.getRoleCode())){
            wrapper.eq("role_code",roleVo.getRoleCode());
        }
        if(Objects.nonNull(roleVo.getStatus())){
            wrapper.eq("status",roleVo.getStatus());
        }
        if(Objects.nonNull(roleVo.getDateRange())&&roleVo.getDateRange().length==2){
            wrapper.between("create_time",roleVo.getDateRange()[0],roleVo.getDateRange()[1]);
        }
        IPage<Role> page1 = roleDao.selectPage(page, wrapper);
        return new Result<>(2000,"查询角色",page1);
    }

    @Override
    public Result<Role> getById(Long id) {
        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.eq("role_id",id);
        roleQueryWrapper.eq("status",0);
        Role role = roleDao.selectOne(roleQueryWrapper);
        return new Result<>(2000,"ok",role);
    }
    @Autowired
    AccountName accountName;
    @Override
    public Result<String> addOrUpd(Role role) {
        String userName = accountName.getAccountName();
        System.out.println("添加或修改的====================== " + role);
        int num;
        if (role.getRoleId()!=null){
            role.setUpdateBy(userName);
            role.setUpdateTime(new Date());
            //修改
            num = roleDao.updateById(role);
        }else {
            role.setCreateBy(userName);
            role.setCreateTime(new Date());
            num = roleDao.insert(role);
        }
        if (num>0){
            return Result.success();
        }
        return Result.error();

    }

    @Override
    public Result<String> deleteByIds(Long[] id) {
        List<Long> collect1 = Arrays.stream(id).collect(Collectors.toList());
        System.out.println("===========多选删除id==============="+collect1);
        int i = roleDao.deleteBatchIds(collect1);
        if (i>0){
            return Result.success();
        }
        return Result.error();
    }
}
